<?php
############## DIT STUK CODE ZORGT ERVOOR DAT JE ALS ADMIN INGELOGD MOET ZIJN OM DE PAGINA TE KUNNEN ZIEN ##############
    //Start de sessie
    session_start();

    //Load de benodigde bestanden
    require_once("includes/auth.php");
########################################################################################################################
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Product toevoegen</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link type="text/css" href="../css/pagination.css" rel="stylesheet" />
  <link rel="stylesheet" href="../css/reset.css" type="text/css" />
  <link rel="stylesheet" href="../css/960.css" type="text/css" />
  <link rel="stylesheet" href="../css/text.css" type="text/css" />
  <link type="text/css" href="../css/tablesorter.css" rel="stylesheet" />
  <link type="text/css" href="../css/sunny/jquery-ui-1.8.custom.css" rel="stylesheet" />
  <link type="text/css" href="../css/jquery.autocomplete.css" rel="stylesheet" />
  <link type="text/css" href="../css/style.css" rel="stylesheet" />
  <link type="text/css" href="../css/accmenu.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery-1.4.2.min.js">
</script>
<script type="text/javascript" src="../js/jquery-ui-1.8.custom.min.js">
</script>
<script type="text/javascript" src="../js/jquery.tablesorter.js">
</script>
<script type="text/javascript" src="../js/jquery.autocomplete.min.js">
</script>
<script type="text/javascript" src="../js/menu.js">
</script>


</head>

<body>
  <div class="container_12">
    <div id="header" class="grid_12">

    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>

    <div id="menu" class="grid_2">

        <?php
        include_once 'temp/menu_filler.php';
        ?>


    </div>

   <div id="adminarea" class="grid_10">
        <div id="tableholder">

              <?php

              // Connects to your Database
              include 'includes/connect.php';

              if (!isset($_POST['submit'])) // if page is not submitted to itself echo the form
              {
              ?>
                  <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                  Naam: <input type="text" name="productnaam" /><br />
                  Omschrijving: <input type="text" name="omschrijving" /><br />
                  Voorraad: <input type="text" name="voorraad" /><br />
                  Prijs: <input type="text" name="stdprijs" /><br />
                  Categorie (cat1): <input type="text" name="categorie" /><br />
                  Afbeelding: <input type="file" name="photo"><br />
                  <input type="submit" name="submit" value="toevoegen">
                  </form>
              <?php
              }
              else
              {
              //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
               function getExtension($str) {
                       $i = strrpos($str,".");
                       if (!$i) { return ""; }
                       $l = strlen($str) - $i;
                       $ext = substr($str,$i+1,$l);
                       return $ext;
               }


              // Max productid zoeken
              $result = mysql_query("SELECT max(productid) FROM product")
              or die(mysql_error());
              $row = mysql_fetch_array( $result );
              $id=$row[0]+1;

              //get the original name of the file from the clients machine
              $filename = stripslashes($_FILES['photo']['name']);
              //get the extension of the file in a lower case format
              $extension = getExtension($filename);
              $extension = strtolower($extension);

              //we will give an unique name, for example the time in unix time format
              $image_name=$id.'.'.$extension;
              //the new name will be containing the full path where will be stored (images folder)
              $newname="../img/".$image_name;

              if(!empty($filename))
              {
                  $pic=$image_name;
              }
              else
              {
                  $pic="no-img.png";
              }

              //Writes the information to the database
              $query = "INSERT INTO product (productid, productnaam, omschrijving, img, voorraad, stdprijs, categorie)
              VALUES
              ('$id','$_POST[productnaam]','$_POST[omschrijving]','$pic','$_POST[voorraad]','$_POST[stdprijs]','$_POST[categorie]')";
              mysql_query($query);



              //Writes the photo to the server
              if(move_uploaded_file($_FILES['photo']['tmp_name'], $newname))
              {

              //Tells you if its all ok
              echo "Product toegevoegd!";
              echo "<br />";
              echo "<a href=\" ../product.php?id=$id \">Bekijk product</a>.";
              }
              else {
              //Tells you if its all ok
              echo "Product toegevoegd!";
              echo "<br />";
              echo "<a href=\" ../product.php?id=$id \">Bekijk product</a>.";
              }


              }
              ?>

</div>
    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>
</div>
</body>
</html>